package com.bed.lovewithpatientbed.service.impl;

import com.bed.lovewithpatientbed.common.bean.LayUiPageBean;
import com.bed.lovewithpatientbed.dao.MalfunctionDao;
import com.bed.lovewithpatientbed.dao.MalfunctionStateDao;
import com.bed.lovewithpatientbed.dao.MalfunctionTypeDao;
import com.bed.lovewithpatientbed.pojo.*;
import com.bed.lovewithpatientbed.service.BedService;
import com.bed.lovewithpatientbed.service.EmployeeService;
import com.bed.lovewithpatientbed.service.HospitalService;
import com.bed.lovewithpatientbed.service.MalfunctionService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author myp
 * @version 1.0.0
 * @ClassName MalfunctionServiceImpl.java
 * @Description 故障记录Service的实现类
 */
@Service
@Transactional
public class MalfunctionServiceImpl implements MalfunctionService {

    @Resource
    private MalfunctionDao malfunctionDao;
    @Resource
    private MalfunctionTypeDao malfunctionTypeDao;
    @Resource
    private MalfunctionStateDao malfunctionStateDao;

    @Resource
    private BedService bedService;
    @Resource
    private EmployeeService employeeService;

    /**
     * @title 新增故障记录
     * @description
     * @author myp
     * @updateTime
     * @throws
     */
    @Override
    public Integer saveMalfunction(Malfunction malfunction) {
        return malfunctionDao.insertSelective(malfunction);
    }
    /**
     * @title 开始维修，增加维修人员信息
     * @description 
     * @author myp 
     * @updateTime   
     * @throws 
     */
    @Override
    public Integer repairMalfunction(Malfunction malfunction, Employee employee) {
        malfunction.setEmployeeId(employee.getEmployeeId());
        return malfunctionDao.updateByPrimaryKeySelective(malfunction);
    }

    @Override
    public Integer getCount(Malfunction malfunction,Date staData , Date endDate) {
        MalfunctionExample malfunctionExample = new MalfunctionExample();
        MalfunctionExample.Criteria malfunctionExampleCriteria = malfunctionExample.createCriteria();
        //根据床的编号查询
        if(malfunction.getBedId() != null && !"".equals(malfunction.getBedId())){
            malfunctionExampleCriteria.andBedIdEqualTo(malfunction.getBedId());
        }
        //根据故障类型查询
        if(malfunction.getMalfunctionTypeId() != null && !"".equals(malfunction.getMalfunctionTypeId())){
            malfunctionExampleCriteria.andMalfunctionTypeIdEqualTo(malfunction.getMalfunctionTypeId());
        }
        //根据医院查询
        if(malfunction.getHospitalId() != null && !"".equals(malfunction.getHospitalId())){
            malfunctionExampleCriteria.andHospitalIdEqualTo(malfunction.getHospitalId() );
            //根据科室查询
            if (malfunction.getDeptId() != null && !"".equals(malfunction.getDeptId())){
                malfunctionExampleCriteria.andDeptIdEqualTo(malfunction.getDeptId());
            }
        }
        //根据故障状态查询
        if(malfunction.getMalfunctionStateId() != null && !"".equals(malfunction.getMalfunctionStateId())){
            malfunctionExampleCriteria.andMalfunctionStateIdEqualTo(malfunction.getMalfunctionStateId());
        }
        //根据日期查询
        if(staData != null && !"".equals(staData)){
            malfunctionExampleCriteria.andMalfunctionDateGreaterThanOrEqualTo(staData);
        }
        if (endDate != null && !"".equals(endDate)){
            malfunctionExampleCriteria.andMalfunctionDateLessThanOrEqualTo(endDate);
        }
        //根据维修人员的编号查找
        if (malfunction.getEmployeeId() != null && !"".equals(malfunction.getEmployeeId() )){
            malfunctionExampleCriteria.andEmployeeIdEqualTo(malfunction.getEmployeeId());
        }

        Long count = malfunctionDao.countByExample(malfunctionExample);
        return count.intValue();
    }

    /**
     * @title 修改故障记录信息
     * @description 修改故障状态
     * @author myp
     * @updateTime
     * @throws
     */
    @Override
    public Integer updateMalfunction(Malfunction malfunction) {
        return  malfunctionDao.updateByPrimaryKeySelective(malfunction);
    }
    /**
     * @title 查找故障记录列表
     * @description 条件组合查询，分页查询
     * @author myp
     * @updateTime
     * @throws
     */
    @Override
    public List<Malfunction> selectMalfunctionList(LayUiPageBean pageBean, Malfunction malfunction, Date staData, Date endDate) {
        MalfunctionExample malfunctionExample = new MalfunctionExample();
        MalfunctionExample.Criteria malfunctionExampleCriteria = malfunctionExample.createCriteria();
        //根据床的编号查询
        if(malfunction.getBedId() != null && !"".equals(malfunction.getBedId())){
            malfunctionExampleCriteria.andBedIdEqualTo(malfunction.getBedId());
        }
        //根据故障类型查询
        if(malfunction.getMalfunctionTypeId() != null && !"".equals(malfunction.getMalfunctionTypeId())){
            malfunctionExampleCriteria.andMalfunctionTypeIdEqualTo(malfunction.getMalfunctionTypeId());
        }
        //根据医院查询
        if(malfunction.getHospitalId() != null && !"".equals(malfunction.getHospitalId())){
            malfunctionExampleCriteria.andHospitalIdEqualTo(malfunction.getHospitalId() );
            //根据科室查询
            if (malfunction.getDeptId() != null && !"".equals(malfunction.getDeptId())){
                malfunctionExampleCriteria.andDeptIdEqualTo(malfunction.getDeptId());
            }
        }
        //根据故障状态查询
        if(malfunction.getMalfunctionStateId() != null && !"".equals(malfunction.getMalfunctionStateId())){
            malfunctionExampleCriteria.andMalfunctionStateIdEqualTo(malfunction.getMalfunctionStateId());
        }
        //根据日期查询
        if(staData != null && !"".equals(staData)){
            malfunctionExampleCriteria.andMalfunctionDateGreaterThanOrEqualTo(staData);
        }
        if (endDate != null && !"".equals(endDate)){
            malfunctionExampleCriteria.andMalfunctionDateLessThanOrEqualTo(endDate);
        }
        //根据维修人员的编号查找
        if (malfunction.getEmployeeId() != null && !"".equals(malfunction.getEmployeeId() )){
            malfunctionExampleCriteria.andEmployeeIdEqualTo(malfunction.getEmployeeId());
        }

        malfunctionExampleCriteria.andSoftdelEqualTo(1);
        if(pageBean.getLimit() != null
                && !"".equals(pageBean.getLimit() )
                && pageBean.getPage() != null
                && !"".equals(pageBean.getPage())){
            malfunctionExample.setOffset((pageBean.getPage() - 1) * pageBean.getLimit());
            malfunctionExample.setLimit(pageBean.getLimit().intValue());
        }

        //获得查询列表
        List<Malfunction> malfunctionList = malfunctionDao.selectByExample(malfunctionExample);
        //嵌套查询
        for(int i=0;i<malfunctionList.size();i++){
            malfunctionList.set(i,setClass(malfunctionList.get(i)));
        }
        return malfunctionList;
    }
    /**
     * @title 查询故障记录
     * @description 根据故障记录编号查找
     * @author myp
     * @updateTime
     * @throws
     */
    @Override
    public Malfunction selectMalfunction(Integer malfunctionId) {
        Malfunction malfunction = setClass(malfunctionDao.selectByPrimaryKey(malfunctionId));
        return malfunction;
    }

    /**
     * @title 嵌套查询实体类
     * @description 
     * @author myp 
     * @updateTime   
     * @throws 
     */
    public Malfunction setClass(Malfunction malfunction){
        //设置床位投放表
        malfunction.setBedPut(bedService.bedSelectBybid(malfunction.getBedId()));
        //设置故障类型
        malfunction.setMalfunctionType(malfunctionTypeDao.selectByPrimaryKey(malfunction.getMalfunctionTypeId()));
        //设置故障状态
        malfunction.setMalfunctionState(malfunctionStateDao.selectByPrimaryKey(malfunction.getMalfunctionStateId()));
        //设置医院信息
        malfunction.setHospital(bedService.bedSelectBybid(malfunction.getBedId()).getHospital());
        //设置科室信息
        malfunction.setDept(bedService.bedSelectBybid(malfunction.getBedId()).getDept());
        //设置雇员信息
        malfunction.setEmployee(employeeService.selectEmployee(malfunction.getEmployeeId()));
        return malfunction;
    }
}
